home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / DB_CLIPP / 2510.ZIP / TRSOURCE.EXE / EXPAND.C < prev    next >
C/C++ Source or Header  |  1990-10-22  |  1KB  |  58 lines

  1. /*********
  2. *  EXPAND.C
  3. *  by Tom Rettig
  4. * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
  5. *
  6. *  Syntax: EXPAND( <expC> [,<expN>] )
  7. *  Return: <expC> with <expN> spaces between each character
  8. *  Note  : If <expN> is omitted, default is one.
  9. *********/
  10.  
  11. #include "trlib.h"
  12.  
  13. TRTYPE expand()
  14. {
  15.    static char funcname[] = { "expand" };
  16.    char *instr, *ret;
  17.    int nspace, instrlen, k;
  18.    long i, j;
  19.    long mbytes;
  20.  
  21.    if ( (PCOUNT==2 && ISCHAR(1) && ISNUM(2)) ||
  22.         (PCOUNT==1 && ISCHAR(1)) )
  23.    {
  24.       instr    = _parc(1);
  25.       nspace   = (PCOUNT==2) ? _parni(2) : 1;
  26.       if (nspace <= 0)
  27.       {
  28.          _retc(instr);
  29.          return;
  30.       }
  31.       instrlen = _tr_strlen(instr);
  32.       mbytes = (long) instrlen * (long) nspace;
  33.       ret      = _tr_allocmem((unsigned)((instrlen+1)+(mbytes)));
  34.  
  35.       if ( ret )
  36.       {
  37.          for ( i=0L, j=0L; instr[i]; i++ )
  38.          {
  39.             ret[j++] = instr[i];
  40.             if ( instr[i+1] )      /* don't expand after last char */
  41.             {
  42.                for ( k=0; k<nspace; k++ )
  43.                   ret[j++] = SPACEC;
  44.             }
  45.          }
  46.  
  47.          ret[j] = NULLC;
  48.  
  49.          _retc( ret );
  50.          _tr_freemem( ret,(unsigned)((instrlen+1)+(mbytes)));
  51.       }
  52.       else  
  53.          _retc( _tr_errmsgs(funcname,E_ALLOC) );
  54.    }
  55.    else
  56.       _retc( _tr_errmsgs(funcname,E_SYNTAX) );
  57. }
  58.